package com.lijie.bs_recruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.Util.WrapperUtil;
import com.lijie.bs_recruit.mapper.NewsMapper;
import com.lijie.bs_recruit.model.News;
import com.lijie.bs_recruit.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijie
 * @since 2021-04-07
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {

    @Autowired
    WrapperUtil wrapperUtil;

    @Override
    public PageInfo<News> selectNews(Integer pageIndex, News news) {
        if(pageIndex == null) pageIndex = 1;
        QueryWrapper<News> Wrapper = wrapperUtil.wrapperNews(news);
        PageHelper.startPage(pageIndex,6);
        List<News> positions = baseMapper.selectList(Wrapper);
        PageInfo<News> pageInfo = new PageInfo<>(positions);
        return pageInfo;
    }

    @Override
    public PageInfo<News> selectNewsByUser(Integer pageIndex, News news) {
        if(pageIndex == null) pageIndex = 1;
        QueryWrapper<News> Wrapper = wrapperUtil.wrapperNews(news);
        Wrapper.eq("isDelete",0  );
        PageHelper.startPage(pageIndex,6);
        List<News> positions = baseMapper.selectList(Wrapper);
        PageInfo<News> pageInfo = new PageInfo<>(positions);
        return pageInfo;
    }

    @Override
    public Boolean addNews(News news) {
        int insert = baseMapper.insert(news);
        return insert>0;
    }

    @Override
    public Boolean updateNews(News news) {
        int insert = baseMapper.updateById(news);
        return insert>0;
    }

    @Override
    public Boolean deleteNews(News news) {
        News news1 = baseMapper.selectById(news.getNewsId());
        UpdateWrapper<News> wrapper=new UpdateWrapper<>();
        if(news1!=null){
            wrapper.set("isDelete",news.getIsDelete()==0?1:0)
                    .eq("newsId", news.getNewsId());
        }
        int update = baseMapper.update(news, wrapper);
        return update>0;
    }

    @Override
    public News selectNewsById(News news) {
        News news1 = baseMapper.selectById(news.getNewsId());
        return news1;
    }

    @Override
    public Boolean addNewsLookNum(News news) {
        //锁
        synchronized (this) {
            News news1 = baseMapper.selectById(news.getNewsId());
            Integer num=news1.getLookNum() + 1;
            news1.setLookNum(num);
            int i = baseMapper.updateById(news1);
            return i > 0;
        }
    }
}
